package zy.blue7.reactor.core.examples.create;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author blue7
 * @version 1.0
 * @date 2022/3/7 11:13
 */
public class CreateSequence {
    public static void main(String[] args) {

    }


    /**
     * 通过任意一种类型 来创建一个序列
     */
    public static void just() {
        List<String> list = new ArrayList<>();
        list.add("abc");
        list.add("defg");
        list.add("ooooo");

        /**
         * just 通过任意类型进行创建序列
         */
        Flux<List<String>> flux = Flux.just(list);

        Flux<Integer> flux1 = Flux.just(9);

        Mono<Integer> mono = Mono.just(6);

        Mono<Object> objectMono = Mono.justOrEmpty(new Object());

        /**
         * 从 Optional 中产生 序列
         */
        Mono<Object> objectMono1 = Mono.justOrEmpty(Optional.empty());


    }


    /**
     * 从不同的 地方获取
     */
    public static void from() {

        /**
         * 从生产者接口中获取数据
         * Supplier<? extends T> supplier
         */
        Mono<Integer> from = Mono.fromSupplier(() -> {
            return 6;
        });

        /**
         * 从一个已存在的 mono中创建序列
         * Publisher<? extends T> source
         */
        Mono<Integer> from1 = Mono.from(from);

        /**
         * 从 Callable 接口中 创建序列
         * Callable<? extends T> supplier
         */
        Mono<Integer> fromCallable = Mono.fromCallable(() -> {
            return 0;
        });

        Mono<Object> objectMono = Mono.fromRunnable(() -> {

        });
//        .....


//        ------------------------------------------------------------------------------------------------------------

        /**
         * 从一个flux中创建序列
         */
        Flux<Integer> from2 = Flux.from(from);

        /**
         * 从一个数组中创建 一个序列
         * T[] array
         */
        String[] array = new String[10];
        Flux<String> stringFlux = Flux.fromArray(array);

        /**
         * 从一个迭代器接口中创建序列
         * Iterable<? extends T> it
         */
        Flux<Object> objectFlux = Flux.fromIterable(new ArrayList<>());


        /**
         * 从一个 整数范围内创建序列
         */
        Flux<Integer> range = Flux.range(1, 100);


        /**
         * 从一个stream 中创建 序列
         * Stream<? extends T> s
         * Supplier<Stream<? extends T>> streamSupplier
         */
        Flux<Object> objectFlux1 = Flux.fromStream(Stream.empty());
        Flux<Object> objectFlux2 = Flux.fromStream(() -> {
            return Stream.empty();
        });


    }

    public static void other() {

        /**
         * 创建一个空序列
         */
        Mono<Object> empty = Mono.empty();
        Flux<Object> empty1 = Flux.empty();


        /**
         * 返回一个error
         */
        Flux<Object> dasdas = Flux.error(new Exception("dasdas"));


        /**
         * 创建一个{@link Flux}，它永远不会发出任何数据、错误或完成信号
         */
        Flux<Object> never = Flux.never();

        /**
         * https://blog.csdn.net/john1337/article/details/104205774
         * 懒汉式 创建，just是饿汉式
         */
        Flux<Date> defer = Flux.defer(() -> Mono.just(new Date()));


//        Flux.using()
    }
}
